'use strict';

const BaseController = require('./base');

class UserInfoController extends BaseController {
  get _service() {
    return 'userInfo';
  }

  async update() {
    const { ctx } = this;
    const { firstName = '', secondName = '', avatar = '', description = '', phoneNum = '', address = '', areaId } = ctx.params();
    const result = await this._getService().update({ firstName, secondName, avatar, description, phoneNum, address, areaId, uid: ctx.userId });
    if (result) {
      this.success();
    } else {
      this.error('更新角色失败');
    }
  }

  async show() {
    await this._show('获取个人信息失败');
  }

  async showUser() {
    const { ctx } = this;
    const { uid } = ctx.params;
    const result = await this._getService().findOne({ uid: +uid });
    if (result) {
      this.success(result);
    } else {
      this.error('获取个人信息失败');
    }
  }

  async create() {
    const { ctx } = this;
    const result = await this._getService().findOne({ uid: ctx.userId });
    if (result) {
      this.success(result);
    } else {
      this.error('获取个人信息失败');
    }
  }

  async index() {
    const { ctx } = this;
    const { firstName, secondName, phoneNum, address, areaId, prop, sort, pageIndex, pageSize } = ctx.params();
    const { createdAt } = ctx.queries;
    const tempResult = await this._getService().findList({ firstName, secondName, phoneNum, address, areaId, prop, sort, pageIndex, pageSize, createdAt });
    if (tempResult) {
      const result = ctx.helper.flat(tempResult.data);
      this.success(result, tempResult.count);
    } else {
      this.error('获取用户信息失败');
    }
  }

  async indexMy() {
    const { ctx } = this;
    const result = await this._getService().findOne({ uid: ctx.userId });
    if (result) {
      this.success(result);
    } else {
      this.error('获取用户信息失败');
    }
  }
}

module.exports = UserInfoController;
